#include "New.h"

//void * new (const void * type, ...)
//{
//	const size_t size = *((const size_t *)type);
//	void * p = malloc(size);
//	assert(p);
//	return p;
//}
//
//void delete (void * item)
//{
//	free(item);
//}



void * new (const void * _type, ...)
{
	const struct TypeDescriptor * type = _type;
	void * p = malloc(type->size);
	assert(p);

	*(const struct TypeDescriptor **) p = type;
	//(struct TypeDescriptor *) p = type;

	if (type->ctor)
	{
		va_list ap;
		va_start(ap, _type);
		p = type->ctor(p, &ap);
		va_end(ap);
	}

	return p;
}


void delete (void * self)
{
	const struct TypeDescriptor ** cp = self;
	
	if (self && * cp && (* cp)->dtor)
		self = (* cp)->dtor(self);
	
	free(self);
}